import 'package:efood_multivendor/helper/responsive_helper.dart';
import 'package:efood_multivendor/main.dart';
import 'package:firebase_analytics/firebase_analytics.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_crashlytics/firebase_crashlytics.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:url_strategy/url_strategy.dart';
import 'dart:async';
import 'dart:io';
import 'package:efood_multivendor/helper/notification_helper.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:get/get.dart';
import 'helper/get_di.dart' as di;
import 'package:flutter_dotenv/flutter_dotenv.dart';

final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
    FlutterLocalNotificationsPlugin();

Future<void> main() async {
  await dotenv.load(fileName: "assets/env/.env.alpha");

  runZonedGuarded<Future<void>>(() async {
    SystemChrome.setPreferredOrientations([
      DeviceOrientation.portraitUp,
      DeviceOrientation.portraitDown,
    ]);
    if (ResponsiveHelper.isMobilePhone()) {
      HttpOverrides.global = new MyHttpOverrides();
    }
    setPathUrlStrategy();
    WidgetsFlutterBinding.ensureInitialized();
    await Firebase.initializeApp();
    await FirebaseAnalytics.instance.logEvent(name: 'app_launch');
    FlutterError.onError = (errorDetails) {
      FirebaseCrashlytics.instance.recordFlutterFatalError(errorDetails);
    };
    Map<String, Map<String, String>> _languages = await di.init();
    try {
      if (GetPlatform.isMobile) {
        await NotificationHelper.initializeForeground(
            flutterLocalNotificationsPlugin);
        FirebaseMessaging.onBackgroundMessage(
            firebaseMessagingBackgroundHandler);
      }
    } catch (e) {}
    SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp])
        .then((_) => runApp(MyApp(
              languages: _languages,
            )));
  }, (error, stack) => FirebaseCrashlytics.instance.recordError(error, stack));
}
